package cn.yanhu.kuwanapp.my.activity

import android.content.Context
import android.content.Intent
import android.view.ViewGroup
import android.widget.TextView
import androidx.activity.viewModels
import androidx.lifecycle.Observer
import cn.woochen.comlib.BaseActivity
import cn.yanhu.kuwanapp.R
import cn.yanhu.kuwanapp.databinding.ActivityMySmallGameBinding
import cn.yanhu.kuwanapp.event.VideoRewardEvent
import cn.yanhu.kuwanapp.my.adapter.MySmallGameAdapter
import cn.yanhu.kuwanapp.user.UserViewModel
import cn.yanhu.kuwanapp.utils.SimpleDialogUtil
import cn.yanhu.kuwanapp.utils.ext.logcom
import cn.yanhu.kuwanapp.utils.ext.parseState
import cn.yanhu.kuwanapp.welfare.WelfareViewModel
import com.jeremyliao.liveeventbus.LiveEventBus
import com.lz.aiwan.littlegame.bean.GameModel
import com.lz.aiwan.littlegame.bean.IGameListCallback
import com.lz.aiwan.littlegame.utils.LzLittleGame

/**
 * 类描述：我的小游戏
 * @author：woochen
 * create at：2021/8/7 10:41 PM
 */
class MySmallGameActivity : BaseActivity<ActivityMySmallGameBinding, UserViewModel>() {

    private val mWelfareViewModel by viewModels<WelfareViewModel>()

    private val mGameListDatas by lazy {
        mutableListOf<GameModel>()
    }



    private val mGameListAdapter by lazy {
        MySmallGameAdapter(mGameListDatas)
    }

    override fun setContentView(): Int = R.layout.activity_my_small_game

    override fun initData() {
        setStatusBarStyle(false)
        initWidgets()
        requestData()
    }

    override fun registerNecessaryObserver() {
        super.registerNecessaryObserver()
        //收到奖励弹窗事件
        LiveEventBus.get(VideoRewardEvent::class.java).observe(this, Observer {
            when (it.source) {
                VideoRewardEvent.SOURCE_SMALL_GAME_LIST -> {
                    mWelfareViewModel.videoReward(it.money,it.businessType,it.gameName)
                }
            }
        })
        //金币奖励领取
        mWelfareViewModel.videoRewardInfo.observe(this, Observer {
            parseState(it, {
                SimpleDialogUtil.create(mContext, layoutRes = R.layout.dialog_receive_coin)?.apply {
                    val llContent = getView<ViewGroup>(R.id.ll_content)
                    val tvGold = getView<TextView>(R.id.tv_gold)
                    tvGold?.text = it.amount
                    llContent?.setOnClickListener {
                        dismiss()
                    }
                }?.show()
            })
        })

    }

    private var page = 1
    override fun requestData() {
        LzLittleGame.getInstance().getPlayedGameListData(mContext, object : IGameListCallback {
            override fun onGameListCallBack(p0: Int, p1: MutableList<GameModel>?, p2: String?) {
                logcom("p0:${p0} p1:${p1?.size} p2:${p2}")
                if (!p1.isNullOrEmpty()){
                    mGameListDatas.clear()
                    mGameListDatas.addAll(p1)
                    mGameListAdapter.notifyDataSetChanged()
                }

            }
        });
    }

    companion object {

        fun start(context: Context) {
            val intent = Intent(context, MySmallGameActivity::class.java)
            context.startActivity(intent)
        }
    }

    private fun initWidgets() {
        mBinding.refreshLayout.setOnRefreshListener {
            it.finishRefresh()
            requestData()
        }
        mBinding.rvGame.run {
            adapter = mGameListAdapter
        }
    }
}